home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************\
-
- File: v find.c
-
- Purpose: This module handles finding text in a fork, and then
- finding it again.
-
-
- Voyeur -- a no-frills file viewer
- Copyright ©1993-4, Mark Pilgrim
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program in a file named "GNU General Public License".
- If not, write to the Free Software Foundation, 675 Mass Ave,
- Cambridge, MA 02139, USA.
-
- \**********************************************************************/
-
- #include "v find.h"
- #include "v.h"
- #include "program globals.h"
- #include "v hex.h"
- #include "v window maintenance.h"
- #include "v file management.h"
- #include "v error.h"
- #include "v progress.h"
- #include "msg graphics.h"
- #include "msg dialogs.h"
- #include "msg prefs.h"
- #include "util.h"
-
- #define PROGRESS_DELAY 120
- #define PROGRESS_COUNT 40
-
- Str255 gFindString;
- Str255 gFindHexString;
- Str255 gFindHexStringInAscii;
- int gBufferOffset[MAX_WINDOWS];
- Boolean gFindHex;
-
- Boolean DoFindAgain(void)
- {
- int charnum;
- unsigned long tempoff;
- Boolean found;
- int bufferoff;
- Str255 theFindStr;
- unsigned long curTicks,startTicks;
- Boolean notDoneYet;
- Boolean progressBarShowing;
- int progressCount;
-
- tempoff=gTheOffset[gWhichFile][gWhichFork[gWhichFile]]+gBufferOffset[gWhichFile];
- bufferoff=gBufferOffset[gWhichFile];
- charnum=1;
- found=FALSE;
- Mymemcpy(theFindStr, gFindHex ? gFindHexStringInAscii : gFindString, 256);
-
- progressBarShowing=FALSE;
- notDoneYet=TRUE;
- startTicks=TickCount();
- while ((notDoneYet) && (!found) &&
- (tempoff<gForkLength[gWhichFile][gWhichFork[gWhichFile]]))
- {
- curTicks=TickCount();
- if ((!progressBarShowing) && (curTicks-startTicks>PROGRESS_DELAY))
- {
- OpenProgressDialog(gForkLength[gWhichFile][gWhichFork[gWhichFile]],
- gFindHex ? "\pFind hex data" : "\pFind text");
- SetProgressText("\pSearching for ", gFindHex ? "\p$" : "\p“",
- gFindHex ? gFindHexString : gFindString, gFindHex ? "\p..." : "\p”...");
- progressCount=0;
- progressBarShowing=TRUE;
- }
-
- if (gTheBuffer[gWhichFile][bufferoff]==(theFindStr[charnum]))
- {
- charnum++;
- if (charnum>theFindStr[0])
- found=TRUE;
- }
- else charnum=1;
-
- if (!found)
- {
- tempoff++;
- bufferoff++;
- if (bufferoff==256)
- {
- GetBuffer(gTheRefNum[gWhichFile], tempoff, gTheBuffer[gWhichFile]);
- bufferoff=0;
- if (progressBarShowing)
- {
- progressCount++;
- if (progressCount==PROGRESS_COUNT)
- {
- UpdateProgressDialog(tempoff);
- notDoneYet=DealWithOtherPeople();
- progressCount=0;
- }
- }
- }
- }
- }
-
- if (progressBarShowing)
- DismissProgressDialog();
-
- if (found)
- {
- gBufferOffset[gWhichFile]=bufferoff;
- if (tempoff%256)
- gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=(tempoff/256)*256;
- else
- gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=tempoff;
- GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
- UpdateProgramWindow(gWhichFile, TRUE);
- return TRUE;
- }
- else if (notDoneYet)
- {
- HandleError(gFindHex ? hexNotFoundErr : asciiNotFoundErr);
- GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
- return FALSE;
- }
- }
-
- Boolean DoFind(Boolean isHex)
- {
- DialogPtr theDlog;
- int itemSelected;
- Handle itemH;
- short item,itemType;
- Rect box;
-
- PositionDialog('DLOG', isHex ? findHexDialog : findDialog);
- theDlog = GetNewDialog(isHex ? findHexDialog : findDialog, 0L, (WindowPtr)-1L);
-
- GetDItem(theDlog, 5, &itemType, &itemH, &box);
- InsetRect(&box, -4, -4);
- SetDItem(theDlog, 5, itemType, (Handle)OutlineDefaultButton, &box);
-
- GetDItem(theDlog,4,&itemType,&itemH,&box);
- SetIText(itemH, isHex ? gFindHexString : gFindString);
- SelIText(theDlog, 4, 0, 32767);
- SetWTitle((WindowPtr)theDlog, isHex ? "\pFind hex data" : "\pFind text");
-
- ShowWindow(theDlog);
-
- itemSelected=0;
- while ((itemSelected!=1) && (itemSelected!=2))
- {
- ModalDialog(isHex ? (ProcPtr)HexOFilter : (ProcPtr)ProcOFilter, &itemSelected);
- }
-
- if (itemSelected==1)
- {
- GetDItem(theDlog,4,&itemType,&itemH,&box);
- GetIText(itemH, isHex ? gFindHexString : gFindString);
- if (isHex)
- {
- if (gFindHexString[0]==0x00)
- itemSelected=2;
- }
- else
- {
- if (gFindString[0]==0x00)
- itemSelected=2;
- }
- }
-
- HideWindow(theDlog);
- DisposeDialog(theDlog);
-
- if ((itemSelected==1) && (isHex))
- {
- if (ValidHex(gFindHexString))
- {
- HexStringToAsciiString(gFindHexString, gFindHexStringInAscii);
- gFindHex=isHex;
- }
- else
- {
- HandleError(invalidHexErr);
- gFindHexString[0]=0x00;
- itemSelected=2;
- }
- }
-
- if (itemSelected==1)
- {
- SaveThePrefs();
- return DoFindAgain();
- }
- return FALSE;
- }
-